package	com.sdry.web.controller.inventoryInfoOnlyCountRejects;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sdry.model.lz.BarCodeConversionRecord;
import com.sdry.service.jyy.ReceiveDetailService;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdry.model.inventoryInfoOnlyCountRejects.InventoryInfoOnlyCountRejects;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.inventoryInfoOnlyCountRejects.InventoryInfoOnlyCountRejectsService;
import com.sdry.service.jyy.UpService;
import com.sdry.service.llm.CancellingStockDetailService;
import com.sdry.utils.DateUtil;
import com.sdry.utils.ResponseUtil;
/**
 *
 *@ClassName: InventoryInfoOnlyCountRejectsController
 *@Description: 精简不良品库存
 *@Author tdd
 *@Date 2019-09-04 15:22:59
 *@version 1.0
*/
@Controller
@RequestMapping("/InventoryInfoOnlyCountRejects")
public class InventoryInfoOnlyCountRejectsController{
	@Resource
	InventoryInfoOnlyCountRejectsService InventoryInfoOnlyCountRejectsService;
	@Resource
	UpService upService;
	@Resource
	CancellingStockDetailService cancellingStockDetailService;
	@Resource
	ReceiveDetailService receiveDetailService;
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageInventoryInfoOnlyCountRejects (Model model) {
		return "/td/InventoryInfoOnlyCountRejects";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public InventoryInfoOnlyCountRejects queryById(Long id) {
		InventoryInfoOnlyCountRejects param = InventoryInfoOnlyCountRejectsService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<InventoryInfoOnlyCountRejects> queryAllByMution(InventoryInfoOnlyCountRejects param) {
		List<InventoryInfoOnlyCountRejects> list = InventoryInfoOnlyCountRejectsService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(InventoryInfoOnlyCountRejects param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<InventoryInfoOnlyCountRejects> list = InventoryInfoOnlyCountRejectsService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = InventoryInfoOnlyCountRejectsService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		ResponseUtil.write(response, jsonObject);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(InventoryInfoOnlyCountRejects param, HttpServletResponse response) throws Exception{
		Long id = InventoryInfoOnlyCountRejectsService.insert(param);
		ResponseUtil.write(response,id);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(InventoryInfoOnlyCountRejects param,HttpServletResponse response) throws Exception{
		Integer count = InventoryInfoOnlyCountRejectsService.update(param);
		ResponseUtil.write(response,count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = InventoryInfoOnlyCountRejectsService.delete(ids);
		ResponseUtil.write(response,count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param mid 主键拼接的字符串
	 * @return 影响行数
	 */
	@RequestMapping("/typeZhuanHuan")
	public void typeZhuanHuan(Long mid, String mBatch,int type, int zhNum, String zhreason, String img_url, HttpSession session, HttpServletResponse response) throws Exception{
		BarCodeConversionRecord record = new BarCodeConversionRecord();
		//获取当前操作人
		ZcSysUserEntity user = (ZcSysUserEntity)session.getAttribute("user");
		int flg = 0;
		//根据物料id+批次查询精简库存
		ZcInventoryInfoEntity zcInventoryInfoEntity = upService.selectByMid2(mid + "", mBatch);
		//根据物料id+批次查询精简不良品库存
		InventoryInfoOnlyCountRejects inventoryInfoOnlyCountRejects = new InventoryInfoOnlyCountRejects();
		inventoryInfoOnlyCountRejects.setMid(mid);
		inventoryInfoOnlyCountRejects.setMbatch(mBatch);
		List<InventoryInfoOnlyCountRejects> inventoryInfoOnlyCountRejectss = InventoryInfoOnlyCountRejectsService.queryAllByMution(inventoryInfoOnlyCountRejects);
		//不良品转良品
		if(type == 0){
			if(inventoryInfoOnlyCountRejectss.size() > 0){
				inventoryInfoOnlyCountRejects = inventoryInfoOnlyCountRejectss.get(0);
				//获取不良品数量
				int mNum = inventoryInfoOnlyCountRejects.getMnum();
				//库存不足
				if(zhNum > mNum){
					flg = -1;
				}else{
					//新增条码转换记录
					record.setMateriel_id(mid);
					record.setMateriel_count(zhNum);
					record.setOperate_id(user.getId());
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					record.setOperate_time(sdf.format(new Date()));
					record.setPrefix_code("");
					record.setOperate_type("精简不良品转良品");
					record.setTrans_reason(zhreason);
					record.setImg_url(img_url);
					//新增条码转换记录
					receiveDetailService.insertBarCodeConversionRecord1(record);
					if(zhNum == mNum){
						InventoryInfoOnlyCountRejectsService.delete(inventoryInfoOnlyCountRejects.getId()+"");
					}else{
						//1.如果是精准码,只存入总数
						mNum = mNum - zhNum;
						//总数量（个数）
						inventoryInfoOnlyCountRejects.setMnum(mNum);
						inventoryInfoOnlyCountRejects.setReason(zhreason);
						inventoryInfoOnlyCountRejects.setImg_url(img_url);
						InventoryInfoOnlyCountRejectsService.update(inventoryInfoOnlyCountRejects);
					}
					//1.如果查到
					if (zcInventoryInfoEntity != null) {
						int mNum1 = zcInventoryInfoEntity.getmNum();
						//1.如果是精准码,只存入总数
						mNum1 = mNum1 + zhNum;
						//总数量（个数）
						zcInventoryInfoEntity.setmNum(mNum1);
						zcInventoryInfoEntity.setReason(zhreason);
						zcInventoryInfoEntity.setImg_url(img_url);
						upService.editStockOnlyCount(zcInventoryInfoEntity);
					} else {
						ZcInventoryInfoEntity zcInventoryInfoEntity1 = new ZcInventoryInfoEntity();
						zcInventoryInfoEntity1.setMid(mid);
						zcInventoryInfoEntity1.setmNum(zhNum);
						zcInventoryInfoEntity1.setmBatch(mBatch);
						zcInventoryInfoEntity1.setEnterPerson(inventoryInfoOnlyCountRejects.getEnterperson());
						zcInventoryInfoEntity1.setEnterDate(inventoryInfoOnlyCountRejects.getEnterdate());
						zcInventoryInfoEntity1.setReason(zhreason);
						zcInventoryInfoEntity1.setImg_url(img_url);
						upService.insertStockOnlyCount(zcInventoryInfoEntity1);
					}
					flg = 1;
				}
				
			}else{
				flg = -1;
			}
		}else{
			//1.如果查到
			if (zcInventoryInfoEntity != null) {
				int mNum = zcInventoryInfoEntity.getmNum();
				//库存不足
				if(zhNum > mNum){
					flg = -1;
				}else{
					//新增条码转换记录
					record.setMateriel_id(mid);
					record.setMateriel_count(zhNum);
					record.setOperate_id(user.getId());
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					record.setOperate_time(sdf.format(new Date()));
					record.setPrefix_code("");
					record.setOperate_type("精简良品转不良品");
					record.setTrans_reason(zhreason);
					record.setImg_url(img_url);
					//新增条码转换记录
					receiveDetailService.insertBarCodeConversionRecord(record);
					if(zhNum == mNum){
						//删除这条数据
						cancellingStockDetailService.deleteInventoryOnlyCountById(zcInventoryInfoEntity.getId());
					}else{
						//1.如果是精准码,只存入总数
						mNum = mNum - zhNum;
						//总数量（个数）
						zcInventoryInfoEntity.setmNum(mNum);
						zcInventoryInfoEntity.setReason(zhreason);
						zcInventoryInfoEntity.setImg_url(img_url);
						upService.editStockOnlyCount(zcInventoryInfoEntity);
					}
					//添加不良品
					if(inventoryInfoOnlyCountRejectss.size() > 0){
						inventoryInfoOnlyCountRejects = inventoryInfoOnlyCountRejectss.get(0);
						int mNum1 = inventoryInfoOnlyCountRejects.getMnum();
						//1.如果是精准码,只存入总数
						mNum1 = zhNum + mNum1;
						//总数量（个数）
						inventoryInfoOnlyCountRejects.setMnum(mNum1);
						inventoryInfoOnlyCountRejects.setReason(zhreason);
						inventoryInfoOnlyCountRejects.setImg_url(img_url);
						InventoryInfoOnlyCountRejectsService.update(inventoryInfoOnlyCountRejects);
					}else{
						inventoryInfoOnlyCountRejects.setMnum(zhNum);
						inventoryInfoOnlyCountRejects.setEnterperson(zcInventoryInfoEntity.getEnterPerson());
						inventoryInfoOnlyCountRejects.setEnterdate(zcInventoryInfoEntity.getEnterDate());
						inventoryInfoOnlyCountRejects.setReason(zhreason);
						inventoryInfoOnlyCountRejects.setImg_url(img_url);
						InventoryInfoOnlyCountRejectsService.insert(inventoryInfoOnlyCountRejects);
					}
					flg = 1;
				}
			} else {
				flg = -1;
			}
		}
		ResponseUtil.write(response,flg);
	}
	/** 
	 * 损益插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/profitAndLoss")
	public void profitAndLoss(InventoryInfoOnlyCountRejects param, HttpServletResponse response) throws Exception{
		Long id = InventoryInfoOnlyCountRejectsService.profitAndLoss(param);
		ResponseUtil.write(response,id);
	}
}
